package com.andatsoft.laisim.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.andatsoft.laisim.theme.Skin;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.Common;
import com.andatsoft.laisim.utils.Utils;
import com.nineoldandroids.view.ViewHelper;

public class BaseActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	protected void setDrawableForView(View view, Drawable drawable) {
		if (drawable == null)
			return;
		if (drawable instanceof BitmapDrawable) {
			if (((BitmapDrawable) drawable).getBitmap().isRecycled())
				return;
		}
		if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD_TO_ICS) {
			view.setBackground(drawable);
		} else {
			view.setBackgroundDrawable(drawable);
		}
	}

	@SuppressLint("NewApi")
	protected void setAlpha(View view, float alpha) {
		if (Utils.androidOs > Common.ANDROID_OS_GINGERBREAD) {
			view.setAlpha(alpha);
		} else {
			ViewHelper.setAlpha(view, alpha);
		}
	}

	public void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			try {
				((ViewGroup) view).removeAllViews();
			} catch (UnsupportedOperationException ue) {

			}
		}
	}

	protected void setBackgroundColorForView(View view) {
		if (getSelectedTheme() != null)
			view.setBackgroundColor(getSelectedTheme()
					.getDialogBackgroundColor() | 0xff000000);
	}

	protected Theme getSelectedTheme() {
		return ThemeManager.getSelectedTheme();
	}

	protected Skin getSelectedSkin() {
		return ThemeManager.getSelectedSkin();
	}

}
